<?php

namespace App\Http\Controllers;

use App\Repositories\DistributeRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class WeChatController extends Controller
{
    protected $DistributeRepository;
    public function __construct()
    {
        $this->DistributeRepository=new DistributeRepository();

    }

    public function serve(){
        $wechat=app('wechat');
        $wechat->server->setMessageHandler(function($message){
            if ($message->MsgType == 'event') {
                switch ($message->Event) {
                    case 'subscribe'://未关注
                        return $this->subscribe($message);
                        break;
                    case 'SCAN':
                        return $this->scan($message);
                    default:
                        return '';
                        break;
                }
            }
            });

        //Log::info('return response.');

        return $wechat->server->serve();
    }
    protected function subscribe($message){
        if(isset($message->EventKey)) {
            //分销
            $leaderId=str_replace('qrscene_','',$message->EventKey);
            $result=$this->DistributeRepository->subscribeDistribute($message->FromUserName,$leaderId);
            if ($result){
                return '感谢关注';
            }
        }
        return '感谢关注';
    }
    protected function scan($message){

        if(isset($message->EventKey)) {
            //分销
            $leaderId=$message->EventKey;
           $result= $this->DistributeRepository->subscribeDistribute($message->FromUserName,$leaderId);
           if ($result){
               return '感谢关注';
           }
        }
        return '感谢关注';
    }
    public function createMenu(){
        $app=app('wechat');
        //$url = 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
          $buttons = [
            [
                "name" => "微网站",
                "sub_button" => [
                    [
                        "type" => "view",
                        "name" => "景区介绍",
                        "url"  => route('wap.about')
                    ],
                    [
                        "type" => "view",
                        "name" => "旅游攻略",
                        "url"  => route('wap.post-list',['slug'=>'lygl'])
                    ]
                ],
            ],
            [
                "name"       => "景点讲解",
                "sub_button" => [
                    [
                        "type" => "view",
                        "name" => "在线讲解",
                        "url"  => route('shake')
                    ],
                    [
                        "type" => "view",
                        "name" => "讲解试听",
                        "url"  => route('scene_index')
                    ]
                ],
            ],
            [
                "name"       => "个人中心",
                "sub_button" => [
                    [
                        "type" => "view",
                        "name" => "老虎机",
                        "url"  => url('game/tiger')
                    ],
                    [
                        "type" => "view",
                        "name" => "购买套餐",
                        "url"  => route('combo_list')
                    ],
                    [
                        "type" => "view",
                        "name" => "促销活动",
                        "url"  => route('activity')
                    ],
                    [
                        "type" => "view",
                        "name" => "我的推广",
                        "url"  => route('user_group')
                    ],
                    [
                        "type" => "view",
                        "name" => "我的信息",
                        "url"  => route('user')
                    ],
                ],
            ],
        ];
        $app->menu->add($buttons);
    }
}
